<?php
namespace app\queue\redis;

use Webman\RedisQueue\Consumer; 
use support\Log;
use Webman\RedisQueue\Redis;
use GuzzleHttp\Client;

class EmailSend implements Consumer
{
    // 要消费的队列名 
    public $queue = 'email_send';
    
    // Redis连接配置
    public $connection = 'default';
    
    // 消费逻辑
    public function consume($data)
    {
        try {    
            $start_time = microtime(true);
            $code = $data['symbol'];
            $hashKey = date("Y-m-d H:i:s", $data['timestamp']/1000);
            $times = $data['trade_volume']/100;
            //发送邮件
            $emailUrl = "http://106.54.231.175/index/shares/sendEmail?code=$code&current=".$data['current']."&percent=".$data['percent']."&times=$times&timestamp=$hashKey";
            $client = new Client();
            $response = $client->get($emailUrl);
            $body = $response->getBody()->getContents();
            Log::info("返回数据: $body");
               
            echo date("Y-m-d H:i:s")."【email-send】-处理{$code}成功"."\r\n"; 
            Log::info("发送邮件成功: $code 耗时：".(microtime(true)- $start_time));
            return true; 
        } catch (\Throwable $e) {
            Log::error("发送邮件失败: {$e->getLine()}  {$e->getMessage()}");
            return false; // 触发重试
        } 
    }
    
} 